package com.sam.chat.api;

import org.springframework.util.ObjectUtils;


/**
 *  @author: sam
 */
public class CommonResult<T> {
    private long code;
    private String message;
    private T data;

    protected CommonResult() {
    }

    public CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResult(long code, String message) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> CommonResult setIErrorCode(IErrorCode iErrorCode) {
        return new CommonResult<T>(iErrorCode.getCode(), iErrorCode.getMessage(), null);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * @Description: 如果null返回失败
     * @Param:
     * @return:
     */
    public static <T> CommonResult<T> successIfNull(T data) {
        if(ObjectUtils.isEmpty(data)){
            return failed();
        }
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * @Description: 如果null返回失败
     * @Param:
     * @return:
     * @Author: Jingxiao.Ye
     * @Date: 2020/11/22
     */
    public static <T> CommonResult<T> successIfNull(T data, IErrorCode iErrorCode) {
        if(ObjectUtils.isEmpty(data)){
            return failed(iErrorCode);
        }
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * @Description: 如果null返回失败
     * @Param:
     * @return:
     */
    public static <T> CommonResult<T> successIfNull(T data, String message) {
        if(ObjectUtils.isEmpty(data)){
            return failed();
        }
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
        if (ObjectUtils.isEmpty(message)) {
            return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
        }
        return new CommonResult<>(errorCode.getCode(), message, null);
    }

    public static <T> CommonResult<T> failed(String message, long code) {
        return new CommonResult<T>(code, message, null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode, T data) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), data);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * @Description: 请求非法
     * @Param:
     * @return:
     */
    public static <T> CommonResult<T> requestIllegalFailed(String message) {
        return new CommonResult<T>(ResultCode.REQUEST_ILLEGAL.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

   /**
    *  @author: L K。
    *  @ClassName:CommonResult
    *  @Date: 2021/10/28 上午10:34
    *  @Description:返回成功
    */

    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

